﻿/*-------------------------------------------------------------------------
* 命名空间: SiliconSelect.ViewModels/HistoryQueryViewModel
* 类       名: HistoryQueryViewModel
* 功      能:  TD
* 时      间:  2023/9/13 16:34:31
* 版      本:  V1.0
* 作      者:  Fioman
* 格      言:  Talk is cheap,show me the code ^_^
*-------------------------------------------------------------------------*/
using SiliconSelect.Base;
using SiliconSelect.BLL;
using SiliconSelect.Common;
using SiliconSelect.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace SiliconSelect.ViewModels
{
    public class HistoryQueryViewModel : NotifyBase
    {
        private ObservableCollection<CrystalResultModel> _crystalResultList =
             new ObservableCollection<CrystalResultModel>();

        public ObservableCollection<CrystalResultModel> CrystalResultList
        {
            get { return _crystalResultList; }
            set { SetProperty(ref _crystalResultList, value); }
        }

        // 检测详情
        public HistoryQueryViewModel()
        {
            InitData(ShowLog: true);
        }


        // 数据初始化,数据刷新的时候调用
        public void InitData(bool ShowLog = false)
        {
            Stopwatch st = new Stopwatch();

            // 开始计时,默认刷新的是当前的晶棒编号的数据.
            st.Start();
            var crystalRecordRes = LocalDataAccess.GetCrystalResult("Id", isDesc: true);
            if (!crystalRecordRes.State)
            {
                LogHelper.WriteLog(LogType.Fatal, crystalRecordRes.Message);
                MessageBox.Show(crystalRecordRes.Message);
            }

            var resultSummaryData = crystalRecordRes.Data!;
            if (resultSummaryData != null && resultSummaryData.Count > 0)
            {
                CrystalResultList.Clear();
                foreach (var result in resultSummaryData)
                {
                    CrystalResultModel model = new CrystalResultModel()
                    {
                        Id = result.Id,
                        CrystalNumber = result.CrystalNumber,
                        ProductionDate = result.ProductionDate,
                        Lv01 = result.Lv01,
                        Lv02 = result.Lv02,
                        Lv03 = result.Lv03,
                        Lv04 = result.Lv04,
                        Lv05 = result.Lv05,
                        Lv06 = result.Lv06,
                        Lv07 = result.Lv07,
                        Lv08 = result.Lv08,
                        Lv09 = result.Lv09,
                        Lv10 = result.Lv10,
                        Lv11 = result.Lv11,
                        Lv12 = result.Lv12,
                        Lv13 = result.Lv13,
                        Lv14 = result.Lv14,
                        Lv15 = result.Lv15,
                        Lv16 = result.Lv16,
                        Lv17 = result.Lv17,
                        Lv18 = result.Lv18,
                        Lv19 = result.Lv19,
                        Lv20 = result.Lv20,
                        Lv21 = result.Lv21,
                        Lv22 = result.Lv22,
                        Lv23 = result.Lv23,
                        Lv24 = result.Lv24,
                        Lv25 = result.Lv25,
                        Lv26 = result.Lv26,
                    };
                    CrystalResultList.Add(model);
                }
            }

            st.Stop();
            //TimeSpan elapsedTime = st.Elapsed;
            if (ShowLog)
            {
                LogHelper.WriteLog(LogType.Info, $"详情页数据加载耗时: {st.ElapsedMilliseconds} ms");
            }
        }
    }
}
